﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AcsCommon;
using System.Data;

namespace YiShi.Class
{
    public class KnowledgePoint: DbObject
    {
        KnowledgePoint[] knowledgePoints;
        public string IdPath
        {
            get { return dr["IdPath"]; }
            set { dr["IdPath"] = value; }
        }
        public string ParentId
        {
            get { return dr["ParentId"]; }
            set { dr["ParentId"] = value; }
        }
        public string Type
        {
            get { return dr["Type"]; }
            set { dr["Type"] = value; }
        }
        public string Name
        {
            get { return dr["Name"]; }
            set { dr["Name"] = value; }
        }
        public string Tip
        {
            get { return dr["Tip"]; }
            set { dr["Tip"] = value; }
        }
        public string NamePath
        {
            get { return dr["NamePath"]; }
            set { dr["NamePath"] = value; }
        }
        public string PointKey
        {
            get { return dr["PointKey"]; }
            set { dr["PointKey"] = value; }
        }
        public string Remark
        {
            get { return dr["Remark"]; }
            set { dr["Remark"] = value; }
        }
        public string SortIndex
        {
            get { return dr["SortIndex"]; }
            set { dr["SortIndex"] = value; }
        }

        public static KnowledgePoint GetRoot(DbAccess db)
        {
            KnowledgePoint kp = new KnowledgePoint(db);
            DataRow dr = db.QueryDataRow("select * from KnowledgePoint where (ParentId is null)");
            dr.FillObject(kp);
            return kp;
        }

        public KnowledgePoint(DbAccess db)
            : base(db, "Admin.KnowledgePoint")
        {
        }
        public override void Update()
        {
            base.Update("Admin.KnowledgePoint");
        }
        public KnowledgePoint InsertChild(string name, string type, string sortIndex)
        {
            KnowledgePoint kp = new KnowledgePoint(db);
            kp.Id = IdFactory.Get("Admin.KnowledgePoint");
            kp.IdPath = this.IdPath + "." + kp.Id;
            kp.ParentId = this.Id;
            kp.Name = name;
            kp.Type = type;
            kp.NamePath = this.NamePath + "." + name;
            kp.SortIndex = sortIndex;
            DataRecord dr = new DataRecord("Admin.KnowledgePoint");
            dr.SetData(kp);
            db.InsertRecord(dr);
            return kp;
        }
        public KnowledgePoint[] GetChild()
        {
            if (String.IsNullOrEmpty(this.Id))
                throw new Exception("The id of the knowledge point is null!");
            if (this.knowledgePoints == null)
            {
                string sql = Sql.Format("select * from KnowledgePoint where (ParentId = '{0}') order by SortIndex", this.Id);
                DataTable dt = this.db.QueryDataTable(sql);
                this.knowledgePoints = new KnowledgePoint[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    this.knowledgePoints[i] = new KnowledgePoint(this.db);
                    dr.FillObject(this.knowledgePoints[i]);
                }
            }
            return this.knowledgePoints;
        }

    }
}
